package ooday04;

import ooday02.Chick;
import ooday02.Dog;
import ooday02.Fish;
import ooday03.ChickDemo;
import ooday03.DogDemo;
import ooday03.Swim;
import sun.font.EAttribute;
import sun.security.provider.ConfigFile;

public class testO {
    public static void main(String[] args) {
        /*

        //演示向上造型（多态）的第二点应用：
        Master master = new Master();
        DogO dog = new DogO("小黑",2,"黑色");
        FishO fisho = new FishO("小花",2,"花色");
        ChickO chickO = new ChickO("小白",2,"白色");
        master.feed(dog);//在传参的同时，系统自动做了向上造型
        master.feed(fisho);
        master.feed(chickO);
        */


       AnimalO[] animalOS = new AnimalO[5];
       animalOS[0] = new DogO("小黑", 5, "黑色");
        animalOS[1] =new DogO("小白", 4, "白");
        animalOS[2] =new FishO("小金", 3, "金");
        animalOS[3] =new DogO("小花", 2, "花");
        animalOS[4] =new DogO("小会", 1, "灰");
        for(int i=0;i<animalOS.length;i++){
            System.out.println(animalOS[i].name);
            animalOS[i].eat();
            animalOS[i].drink();

            if (animalOS[i] instanceof DogO){//狗类型
                DogO dog = (DogO) animalOS[i];
                dog.swim();
                dog.lookHome();
            }
            if (animalOS[i] instanceof ChickO){//鸡类型
                ChickO chickO = (ChickO) animalOS[i];
                chickO.layEgg();
            }
            if(animalOS[i] instanceof FishO){//强转鱼类型
                FishO f = (FishO) animalOS[i];
                f.swim();
                f.drink();
            }
            if (animalOS[i] instanceof Swim){//使用范围更广
                Swim s = (Swim)animalOS[i];
                s.swim();
            }
        }
        /*
        AnimalO o = new DogO("小黑",2,"黑色");
        DogO d = (DogO)o;
        FishO f = (FishO)o;
        Swim s = (Swim) o;
        System.out.println(o instanceof DogO);
        System.out.println(o instanceof FishO);
        System.out.println(o instanceof Swim);*/


    }
}
